#include "FKClientObjectManager.h"

#include "FKClientManager.h"
#include "FKEvent.h"
#include "FKPacker.h"
#include "FKClient.h"
#include "FKClientData.h"
#include "FKFileReciever.h"

#include <QDataStream>
#include <QTcpSocket>
//#include <QByteArray>

#include "../FKTools/FKLogger.h"

FKClientObjectManager::FKClientObjectManager():FKAbstractObjectManager(){
    FK_CBEGIN
    _server=new FKClientManager;
    _fileReciever=new FKFileReciever(_server);
    connect(_server,SIGNAL(fileMessage(QVariant)),_fileReciever,SLOT(processAnswer(QVariant)));
    connect(_fileReciever,SIGNAL(errorMessage(QString,QString)),SLOT(errorMessage(QString,QString)));
    connect(_fileReciever,SIGNAL(fileRecieved(QString)),SIGNAL(fileRecieved(QString)));
    connect(_fileReciever,SIGNAL(fileError(QString)),SIGNAL(fileError(QString)));
    connect(_fileReciever,SIGNAL(bytesRecieved(QString,qint64)),SIGNAL(bytesRecieved(QString,qint64)));
    connect(_fileReciever,SIGNAL(fileRequest(QVariant)),_server,SLOT(sendFileMessage(QVariant)));
    connect(_fileReciever,SIGNAL(fileError(QString)),_server,SLOT(sendInterruptFileMessage(QString)));
    connect(_server,SIGNAL(message(QString)),SLOT(showMessage(QString)));
    connect(_server,SIGNAL(message(QString,FKClientManager*)),SLOT(sendMessage(QString,FKClientManager*)));
    connect(_server,SIGNAL(gotEvent(FKEvent*)),SLOT(externalEvent(FKEvent*)));
    connect(_server,SIGNAL(clientSetted(FKClient*)),SLOT(setLocalClient(FKClient*)));
    connect(_server,SIGNAL(disconnected(FKClientManager*)),SIGNAL(disconnected()));
    FK_CEND
}

FKClientObjectManager::~FKClientObjectManager(){
    FK_DBEGIN
    _server->deleteLater();
    FK_DEND
}

bool FKClientObjectManager::connectToServer(QString ip, int port){
    _server->socket()->connectToHost(ip,port);
    return _server->socket()->waitForConnected(5000);
}

void FKClientObjectManager::action(FKEvent* action){
    QByteArray data(FKClientManager::preserveData(FKPacker::pack(FKPacker::action, action)));
    _server->sendData(data);
    action->deleteLater();
}

void FKClientObjectManager::externalEvent(FKEvent* event){
    if(getLocalClient()){
        getLocalClient()->manageEvent(event);
    }else{
        returnMessage(event->client(),QString("error proccessing external event by client object manager"));
        event->deleteLater();
    }
}

void FKClientObjectManager::clientRequested(QString clientName, QString password){
    FKClientData clientData(clientName,password,QVariant());
    QByteArray client(FKClientManager::preserveData(FKPacker::pack(FKPacker::clientRequest,&clientData)));
    _server->sendData(client);
}

void FKClientObjectManager::genClient(QString clientName, QString password, QVariant validator){
    FKClientData clientData(clientName,password,validator);
    QByteArray client(FKClientManager::preserveData(FKPacker::pack(FKPacker::genClient,&clientData)));
    _server->sendData(client);
}

void FKClientObjectManager::getClient(QString clientName, QString password, FKClientManager *s){
    Q_UNUSED(clientName)
    Q_UNUSED(password)
    Q_UNUSED(s)
//    setLocalClient(s->cl);
//    emit clientSetted(getLocalClient());
    FK_MLOG("client object manager getClient function reached")
}

void FKClientObjectManager::returnMessage(qint32 clientID, QString text){
    Q_UNUSED(clientID)
    showMessage(text);
}

void FKClientObjectManager::fileRequest(QString fl){
    _fileReciever->requestFile(fl);
}

void FKClientObjectManager::interruptFilesDownloading(){
    _fileReciever->interruptDownloading();
}

void FKClientObjectManager::errorMessage(QString file, QString message){
    showMessage(QString(tr("Downloading file error %1: %2")).arg(file).arg(message));
}
